﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_GroupAdmin
{
    public partial class Edit : AdminPage
    {
        private GroupAdmin curGroupAdmin = new GroupAdmin();
        private GroupPages curGroupPages = new GroupPages();
        private GroupAdminInfo model = null;
        private string groupname = "";
        private string power = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            //权限检测
            this.CheckPower();

            power = (Request.QueryString["power"] == null) ? "" : Request.QueryString["power"].Trim();
            if (!IsPostBack)
            {
                //上次请求路径
                if (hidReferrer.Value.Trim() == "")
                {
                    Uri uri = Request.UrlReferrer;
                    if (uri != null)
                    {
                        hidReferrer.Value = uri.ToString();
                    }
                }
                try
                {
                    if (power != "")
                    {
                        model = curGroupAdmin.GetGroupAdminInfo(power);
                    }
                    Repeater_admin_bind();
                }
                catch (Exception ex) { Response.Redirect("../Message.aspx?message=绑定异常！" + ex.Message + "&z=no"); }
            }
        }


        #region 数据绑定
        void Repeater_admin_bind()
        {
            if (model != null)
            {
                txtName.Text = model.name;
            }
            DataTable dt = curGroupPages.GetList();

            Repeater_admin.DataSource = dt;
            Repeater_admin.DataBind();
        }
        #endregion

        #region 显示数据处理
        protected void Repeater_admin_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal litTitle = (Literal)e.Item.FindControl("litTitle");
                CheckBox Code_list = (CheckBox)e.Item.FindControl("Code_list");
                HiddenField hidCodeno = (HiddenField)e.Item.FindControl("hidCodeno");

                DataRowView drv = (DataRowView)e.Item.DataItem;
                string tgroupname = drv["groupname"].ToString();
                if (groupname != tgroupname)
                {
                    litTitle.Text = "</ul><ul class='codelist'><li><b>" + tgroupname + "</b></li></ul><ul class='codelist'>";
                }
                else
                {
                    litTitle.Text = "";
                }
                groupname = tgroupname;
                string codeno = drv["codeno"].ToString();
                Code_list.Text = drv["pagename"].ToString();
                if (model != null && model.codelist.Contains(codeno+","))
                {
                    Code_list.Checked = true;
                }
                hidCodeno.Value = codeno;
            }
        }
        #endregion


        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int nub = 0;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                CheckBox Code_list = (CheckBox)Repeater_admin.Items[i].FindControl("Code_list");
                HiddenField hidCodeno = (HiddenField)Repeater_admin.Items[i].FindControl("hidCodeno");
                if (Code_list.Checked)
                {
                    sb.Append(hidCodeno.Value.Trim() + ",");
                    nub++;
                }
            }
            if (sb.ToString() != "")
            {
                GroupAdminInfo model = new GroupAdminInfo();
                model.power = power;
                model.name = txtName.Text.Trim();
                model.codelist = sb.ToString();
                if (curGroupAdmin.Update(model))
                {
                    string refurl = hidReferrer.Value.Trim();
                    if (refurl == "" || refurl == this.Request.Path.Trim())
                    {
                        Response.Redirect("../Message.aspx?message=修改成功！" + "&z=yes");
                    }
                    else
                    {
                        Response.Redirect(refurl);
                    }
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=修改失败！&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改失败！&z=no");
            }
        }
    }
}
